<?php
/**
 * 
 * @authors 玉坤函数库(you@example.org)
 * @date    2016-08-16 14:22:51
 * @version 1.0
 */
function Post($curlPost,$url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    $return_str = curl_exec($curl);
    curl_close($curl);
    return $return_str;
}
function xml_to_array($xml){
  $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
  if(preg_match_all($reg, $xml, $matches)){
    $count = count($matches[0]);
    for($i = 0; $i < $count; $i++){
    $subxml= $matches[2][$i];
    $key = $matches[1][$i];
      if(preg_match( $reg, $subxml )){
        $arr[$key] = xml_to_array( $subxml );
      }else{
        $arr[$key] = $subxml;
      }
    }
  }
  return $arr;
}
function random($length = 6 , $numeric = 0) {
  PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  if($numeric) {
    $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  } else {
    $hash = '';
    $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
    $max = strlen($chars) - 1;
    for($i = 0; $i < $length; $i++) {
      $hash .= $chars[mt_rand(0, $max)];
    }
  }
  return $hash;
}




function GetRandStr($len) 
{ 
    $chars = array( 
        // "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",  
        // "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",  
        // "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",  
        // "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",  
        // "S", "T", "U", "V", "W", "X", "Y", "Z",
         "1", "2",  
        "3", "4", "5", "6", "7", "8", "9" 
    ); 
    $charsLen = count($chars) - 1; 
    shuffle($chars);   
    $output = ""; 
    for ($i=0; $i<$len; $i++) 
    { 
        $output .= $chars[mt_rand(0, $charsLen)]; 
    }  
    return $output;  
} 

//格式化时间
 function getTime($time)
        {
      return $da=date("Y-m-d H:i:s",$time);  
        }
        function toDate($time, $format = 'Y-m-d H:i:s') {
  if (empty ( $time )) {
    return '';
  }
  $format = str_replace ( '#', ':', $format );
  return date ($format, $time );
}

function getStatus($status, $imageShow = true) {
  switch ($status) {
    case 0 :
      $showText = '禁用';
      $showImg = '<IMG SRC="/Public/Rbac/images/locked.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="禁用">';
      break;
    case 2 :
      $showText = '待审';
      $showImg = '<IMG SRC="/Public/Rbac/images/prected.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="待审">';
      break;
    case - 1 :
      $showText = '删除';
      $showImg = '<IMG SRC="/Public/Rbac/images/del.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="删除">';
      break;
    case 1 :
    default :
      $showText = '正常';
      $showImg = '<IMG SRC="/Public/Rbac/images/ok.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="正常">';

  }
  return ($imageShow === true) ?  $showImg  : $showText;

}

function getNodeGroupName($id) {
  if (empty ( $id )) {
    return '未分组';
  }
  if (isset ( $_SESSION ['nodeGroupList'] )) {
    return $_SESSION ['nodeGroupList'] [$id];
  }
  $Group = D ( "Group" );
  $list = $Group->getField ( 'id,title' );
  $_SESSION ['nodeGroupList'] = $list;
  $name = $list [$id];
  return $name;
}

function showStatus($status, $id) {
  switch ($status) {
    case 0 :
      $info = '<a href="javascript:resume(' . $id . ')">恢复</a>';
      break;
    case 2 :
      $info = '<a href="javascript:pass(' . $id . ')">批准</a>';
      break;
    case 1 :
      $info = '<a href="javascript:forbid(' . $id . ')">禁用</a>';
      break;
    case - 1 :
      $info = '<a href="javascript:recycle(' . $id . ')">还原</a>';
      break;
  }
  return $info;
}


function getGroupName($id) {
  if ($id == 0) {
    return '无上级组';
  }
  if ($list = F ( 'groupName' )) {
    return $list [$id];
  }
  $dao = D ( "Role" );
  $list = $dao->select( array ('field' => 'id,name' ) );
  foreach ( $list as $vo ) {
    $nameList [$vo ['id']] = $vo ['name'];
  }
  $name = $nameList [$id];
  F ( 'groupName', $nameList );
  return $name;
}

function pwdHash($password, $type = 'md5') {
  return hash ( $type, $password );
}

 function dele($id,$table) {
        if (!empty($id)) {
            $Form = M("$table");
            $result = $Form->delete($id);
            if (false !== $result) {
                echo '删除成功！';
            } else {
                echo '删除出错！';
            }
        } else {
            $this->error('ID错误！');
        }
    }
    /*  ---- 取出当前id 下的所有子权限节点  
     *  传入空字符串取地址
     *  空字符串会返回一个带 , 的id字符串
     *  适用于 对id 删除 并没有取出 尾部 逗号
     */ 
    function nodeselect($nlist,&$str,$n=0){
        foreach($nlist as $k=>$v){
    
            if ($v['pid']==$n) {
                $str .= $v['id'].",";
                nodeselect($nlist,$str,$v['id']);
            }
        }
    }
    
    //------------- 将所有权限节点重新排序 显示层次感 ------------------//
    function nAllselect($nlist,$n=0){
        $nodearr = array();
        foreach($nlist as $nv){
    
            if ($nv['pid']==$n) {
                $nodearr[] = array(
                        "title" => $nv['title'],
                        "id" => $nv['id'],
                        "children" => nAllselect($nlist,$nv['id'])
                );
            }
        }
        return $nodearr;
    }
    
    // --- 将 权限节点 数组按等级重新排列 --- //
    function nselect($nlist,&$arr,$m=0,$n=0){
            
        $nbsp   = str_repeat("&nbsp;&nbsp;",$m*2);
        $m++;
        foreach($nlist as $v){
            if ($v['pid'] == $n) {
                $arr[] = array(
                        'name'    => $v['name'],
                        'title'    => $v['title'],
                        'id'    => $v['id'],
                        "nbsp"  => $nbsp
                );
                nselect($nlist,$arr,$m,$v['id']);
            }
        }
    }
//处理方法
function rmdirr($dirname) {
    if (!file_exists($dirname)) {
        return false;
    }
    if (is_file($dirname) || is_link($dirname)) {
        return unlink($dirname);
    }
    $dir = dir($dirname);
    if ($dir) {
        while (false !== $entry = $dir->read()) {
            if ($entry == '.' || $entry == '..') {
                continue;
            }
            //递归
            rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
        }
    }
}

//公共函数
//获取文件修改时间
function getfiletime($file, $DataDir) {
    $a = filemtime($DataDir . $file);
    $time = date("Y-m-d H:i:s", $a);
    return $time;
}

//获取文件的大小
function getfilesize($file, $DataDir) {
    $perms = stat($DataDir . $file);
    $size = $perms['size'];
    // 单位自动转换函数
    $kb = 1024;         // Kilobyte
    $mb = 1024 * $kb;   // Megabyte
    $gb = 1024 * $mb;   // Gigabyte
    $tb = 1024 * $gb;   // Terabyte

    if ($size < $kb) {
        return $size . " B";
    } else if ($size < $mb) {
        return round($size / $kb, 2) . " KB";
    } else if ($size < $gb) {
        return round($size / $mb, 2) . " MB";
    } else if ($size < $tb) {
        return round($size / $gb, 2) . " GB";
    } else {
        return round($size / $tb, 2) . " TB";
    }
}

